package mysql

import (
	"time"

	"gorm.io/gorm"
)

// Job represents a job in the system
type Job struct {
	ID         uint           `gorm:"primaryKey;autoIncrement" json:"id"`
	JobID      string         `gorm:"uniqueIndex;not null;size:255" json:"job_id"`
	JobType    string         `gorm:"not null;size:100" json:"job_type"`
	Status     string         `gorm:"not null;default:'pending';size:50" json:"status"`
	Priority   int            `gorm:"not null;default:1" json:"priority"`
	Parameters string         `gorm:"type:text" json:"parameters"` // JSON string
	CreatedAt  time.Time      `gorm:"not null" json:"created_at"`
	UpdatedAt  time.Time      `gorm:"not null" json:"updated_at"`
	DeletedAt  gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`

	// Relationships
	JobClusters []JobCluster `gorm:"foreignKey:JobID;references:JobID" json:"job_clusters,omitempty"`
}

// Cluster represents a Kubernetes cluster
type Cluster struct {
	ID          uint           `gorm:"primaryKey;autoIncrement" json:"id"`
	ClusterID   string         `gorm:"uniqueIndex;not null;size:255" json:"cluster_id"`
	Name        string         `gorm:"not null;size:255" json:"name"`
	Kubeconfig  string         `gorm:"type:text;not null" json:"kubeconfig"` // Base64 encoded
	Status      string         `gorm:"not null;default:'active';size:50" json:"status"`
	Description string         `gorm:"type:text" json:"description"`
	CreatedAt   time.Time      `gorm:"not null" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"not null" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`

	// Relationships
	JobClusters []JobCluster `gorm:"foreignKey:ClusterID;references:ClusterID" json:"job_clusters,omitempty"`
}

// JobCluster represents the many-to-many relationship between jobs and clusters
type JobCluster struct {
	ID          uint           `gorm:"primaryKey;autoIncrement" json:"id"`
	JobID       string         `gorm:"not null;size:255;index" json:"job_id"`
	ClusterID   string         `gorm:"not null;size:255;index" json:"cluster_id"`
	Status      string         `gorm:"not null;default:'pending';size:50" json:"status"`
	Message     string         `gorm:"type:text" json:"message"`
	StartedAt   *time.Time     `json:"started_at"`
	CompletedAt *time.Time     `json:"completed_at"`
	CreatedAt   time.Time      `gorm:"not null" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"not null" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`

	// Composite unique index
	UniqueJobCluster string `gorm:"uniqueIndex:idx_job_cluster;not null" json:"-"`
}

// BeforeCreate sets the unique constraint value
func (jc *JobCluster) BeforeCreate(tx *gorm.DB) error {
	jc.UniqueJobCluster = jc.JobID + ":" + jc.ClusterID
	return nil
}

// BeforeUpdate sets the unique constraint value
func (jc *JobCluster) BeforeUpdate(tx *gorm.DB) error {
	jc.UniqueJobCluster = jc.JobID + ":" + jc.ClusterID
	return nil
}

// JobStatus represents job status history
type JobStatus struct {
	ID        uint           `gorm:"primaryKey;autoIncrement" json:"id"`
	JobID     string         `gorm:"not null;size:255;index" json:"job_id"`
	ClusterID string         `gorm:"not null;size:255;index" json:"cluster_id"`
	Status    string         `gorm:"not null;size:50" json:"status"`
	Message   string         `gorm:"type:text" json:"message"`
	Details   string         `gorm:"type:text" json:"details"` // JSON string
	CreatedAt time.Time      `gorm:"not null" json:"created_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
}

// ClusterHealth represents cluster health monitoring data
type ClusterHealth struct {
	ID          uint           `gorm:"primaryKey;autoIncrement" json:"id"`
	ClusterID   string         `gorm:"not null;size:255;index" json:"cluster_id"`
	Status      string         `gorm:"not null;size:50" json:"status"`
	CPUUsage    float64        `gorm:"type:decimal(5,2)" json:"cpu_usage"`
	MemoryUsage float64        `gorm:"type:decimal(5,2)" json:"memory_usage"`
	NodeCount   int            `gorm:"not null;default:0" json:"node_count"`
	Message     string         `gorm:"type:text" json:"message"`
	CreatedAt   time.Time      `gorm:"not null" json:"created_at"`
	DeletedAt   gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`
}

// TableName returns the table name for Job
func (Job) TableName() string {
	return "jobs"
}

// TableName returns the table name for Cluster
func (Cluster) TableName() string {
	return "clusters"
}

// TableName returns the table name for JobCluster
func (JobCluster) TableName() string {
	return "job_clusters"
}

// TableName returns the table name for JobStatus
func (JobStatus) TableName() string {
	return "job_statuses"
}

// TableName returns the table name for ClusterHealth
func (ClusterHealth) TableName() string {
	return "cluster_health"
}
